package org.example.service.imp;

import org.example.dao.imp.UserDaoImp;
import org.example.entitys.systems.User;
import org.example.service.inter.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/12
 */

@Service
public class UserServiceImp implements UserService {
    @Autowired
    UserDaoImp userDaoImp;

    @Override
    public boolean insertUser(User user) {
        return userDaoImp.insert(user) == 1;
    }

    @Override
    public boolean updateUser(User user) {
        if (!userDaoImp.isExitById(user.getId())) return false;
        return userDaoImp.update(user) == 1;
    }

    @Override
    public int isLogin(int id, String password) {
        if (!userDaoImp.isExitById(id)) return 1;//判断id是否存在
        User user = userDaoImp.queryById(id);//获取用户User类
        if (!user.getPassword().equals(password)) return 2;//判断密码是否正常
        if (user.getStatus() == 2) return 3;//判断账户信息是否正常
        return 0;
    }

    @Override
    public boolean isAllowPermission(int user_id, int perm_id) {
        if (!userDaoImp.isExitById(user_id) || !userDaoImp.isExitPermById(perm_id)) return false;
        return userDaoImp.isAllowPermission(user_id, perm_id);
    }

    @Override
    public boolean unavailableUser(int id) {
        if (!userDaoImp.isExitById(id)) return false;
        return userDaoImp.unavailableUser(id);
    }

    @Override
    public User queryUserById(int id) {
        if (!userDaoImp.isExitById(id)) return null;
        return userDaoImp.queryById(id);
    }

    @Override
    public User[] queryUserByName(String name) {
        return userDaoImp.queryByName(name).toArray(new User[0]);
    }

    @Override
    public User[] queryUserByArgs(String login_name, String gender, String birthday, String phone, String email, int status) {
        return new User[0];
    }

    @Override
    public User[] queryUserAll() {
        return userDaoImp.queryAll().toArray(new User[0]);
    }
}
